<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<%@ include file="/WEB-INF/views/include/head.jsp"%>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>创建款式类别</title>
<script src="${ctxStatic}/jquery/jquery-1.9.1.min.js"></script>
<script src="${ctxStatic}/js/DateTimePicker.js"></script>
<link rel="stylesheet" href="${ctxStatic}/css/layerSm.css"> 
<script type="text/javascript">
	var windowH = window.innerHeight || document.documentElement.clientHeight
			|| document.body.clientHeight
	$('.middle').css('height', windowH - 50);

	// 正则表达式，判断0到9字母a到z下划线_
	var lettersnumbers = /^[0-9a-zA-Z_\s?]*$/;

	$(document).ready(
			function() {

				// 判断子项是否为空 添加class
				if ($("#listLength").val() > 0) {

					$("#contain").addClass('tabScroll');

					var theadW = $('.tabScroll thead').width()
					var theadTh = $('.tabScroll thead tr th').length
					$('.tabScroll thead tr th').width(theadW / theadTh)
					$('.tabScroll tbody tr:first-child td').each(
							function() {
								var indexNum = $(this).index()
								var tdWidth = $(
										'.tabScroll tbody tr:first-child td')
										.eq(indexNum).css('width')
								$('.tabScroll thead tr th').eq(indexNum).width(
										tdWidth);
							})
				}
			});

	//保存方法
	var saveForm = {
		s : null,
		t : null,
		save : function(s, t) {
			$(t).attr("disabled", true);
			this.s = s;
			this.t = t;
			// 表单验证
			$("#inputForm")
					.validate(
							{
								submitHandler : function() {
									// 从表单中获取数据
									var params = $("#inputForm").serialize();
									// 通过调用ajax 方法调用后台方法
									$
											.post(
													"${ctx}/material/category/save",
													params,
													function(result) {
														// 提示信息
														tipParent(result.message);
														// 返回状态
														if (result.statusCode == STATUSCODEOK) {
															if (saveForm.s == 1) {
																// 重置表单
																$("#inputForm")[0]
																		.reset();
																$(saveForm.t)
																		.attr(
																				"disabled",
																				false);
																// 刷新页面
																window.parent.frames[0].location
																		.reload();
																addItemValue();
															} else {
																// 刷新页面
																window.parent.frames[0].location
																		.reload();
																closeWin();
															}
														} else {
															$(saveForm.t).attr(
																	"disabled",
																	false);
														}
													}, 'json');
								},
								// 校验是否重名
								rules : {
									itemValue : {
										remote : "${ctx}/material/message/checkName?oldItemValue="
												+ $("#oldItemValue").val()
									},
								},
								messages : {
									//分类值已存在
									itemValue : {
										remote : '<spring:message code="categoryItemValue_exist"/>'
									},
								},
								fail : function(element, e) {
									$(saveForm.t).attr("disabled", false);
								}
							});
			// 提交表单
			$("#inputForm").submit();
		}
	}

	// 删除子项目
	function deleteSub(delIndex) {
		// 调用后台删除子项方法
		$.post("${ctx}/material/message/deleteSub?delIndex=" + delIndex, "",
				function(result) {
					// 回调函数赋值
					var h = $(callBackHtml(result)).find("#subItem").html();

					$('#subItem').html(h);

					// 添加class
					$("#contain").addClass('tabScroll');

					var theadW = $('.tabScroll thead').width()
					var theadTh = $('.tabScroll thead tr th').length
					$('.tabScroll thead tr th').width(theadW / theadTh)
					$('.tabScroll tbody tr:first-child td').each(
							function() {
								var indexNum = $(this).index()
								var tdWidth = $(
										'.tabScroll tbody tr:first-child td')
										.eq(indexNum).css('width')
								$('.tabScroll thead tr th').eq(indexNum).width(
										tdWidth);
							})

				}, 'html');

	}

	// 新建子项
	function createSubItem(delIndex) {
		// 项目值不能为空
		if ($('#itemValue').val() != null
				&& $('#itemValue').val() != 'null'
				&& $('#itemValue').val().trim() != ''
				&& $('#itemCode').val().trim() != $('#itemValue').val().trim() != ''
				&& lettersnumbers.test($("#itemValue").val())) {

			popup('800px', '470px', '${ctx}/material/message/formSub?itemCode='
					+ $('#itemValue').val() + "&delIndex=" + delIndex);

		} else {

			// 请输入合法项目值！
			layer
					.msg(
							'<spring:message code="Please_enter_the_legal_project_value"/>',
							{
								time : 1000
							});

		}

	}

	//选择添加子项目
	function addItemValue() {
		// 调用后台查询子项list
		$.post("${ctx}/material/message/searchSubList?itemValue = "
				+ $('#itemValue').val(), "", function(result) {
			// 回调函数赋值
			var h = $(callBackHtml(result)).find("#subItem").html();
			$('#subItem').html(h);

			$("#contain").addClass('tabScroll');

			var theadW = $('.tabScroll thead').width()
			var theadTh = $('.tabScroll thead tr th').length
			$('.tabScroll thead tr th').width(theadW / theadTh)
			$('.tabScroll tbody tr:first-child td')
					.each(
							function() {
								var indexNum = $(this).index()
								var tdWidth = $(
										'.tabScroll tbody tr:first-child td')
										.eq(indexNum).css('width')
								$('.tabScroll thead tr th').eq(indexNum).width(
										tdWidth);
							})
		}, 'html');

	}

	// 子项值是否重复
	function isSubItemValueRepeate(itemValue, delIndex) {

		var number = 0;
		// 页面获取值比较
		$("[name='subItemValues']").each(function(i) {
			if (itemValue.trim() == $(this).val().trim() && delIndex != i) {
				number = number + 1;
			}
		});

		if (number > 0) {
			return true;
		} else {
			return false;
		}

	}
</script>
</head>
<body>
	<div class="layerHeader clearfix">
		<p class="headerTitle">
			<c:choose>
				<c:when test="${category.id!=null&&category.id!=''}">
					<!--编辑-->
					<spring:message code="category_modifyCategory" />
				</c:when>
				<c:otherwise>
					<!--新建-->
					<spring:message code="category_createCategory" />
				</c:otherwise>
			</c:choose>
		</p>
		<div class="headerButton clearfix">
			<shiro:hasPermission name="material:stock:edit">
				<!--分类确认-->
				<button onclick="saveForm.save(0,this)">
					<spring:message code="category_save" />
				</button>
				<!--分类确认并继续-->
				<c:if test="${empty category.id}">
					<button onclick="saveForm.save(1,this)">
						<spring:message code="category_saveAndcontinue" />
					</button>
				</c:if>
			</shiro:hasPermission>
			<!--取消-->
			<button onclick="closeWin()">
				<spring:message code="category_close" />
			</button>
		</div>
	</div>

	<div class="smList">
		<div class="items clearfix">
			<div class="col-50 clearfix">
				<!--分类CD-->
				<p class="tit">
					<spring:message code="category_edit_code" />
				</p>
				<input value="${category.itemCode}" name="itemCode">
			</div>
		</div>
		<div class="items clearfix">
			<div class="col-50 clearfix">
				<!--数据值-->
				<p class="tit">
					<spring:message code="category_show_itemValue" />
				</p>
				<input value="SHIRT ">
			</div>
		</div>
		<div class="items clearfix">
			<div class="col-50 clearfix">
				<!--中文名称-->
				<p class="tit">
					<spring:message code="category_show_Cn" />
				</p>
				<input type="text" id="itemNameCn" name="itemNameCn"
					value="${category.itemNameCn}" maxlength="50"
					data-rule-required="true"
					data-msg-required="<spring:message code="category_pleaseInputCn"/>"
					class="form-control fam">
			</div>
			<div class="col-50 right clearfix">
				<!--中文略称-->
				<p class="tit">
					<spring:message code="category_show_abbreviation_Cn" />
				</p>
				<input type="text" id="itemNameCn" name="itemNameCn"
					value="${category.itemNameCn}" maxlength="50"
					data-rule-required="true"
					data-msg-required="<spring:message code="category_pleaseInput_abbreviationCn"/>"
					class="form-control fam">
			</div>
		</div>
		<div class="items clearfix">
			<div class="col-50 clearfix">
				<p class="tit">
					<!--英文名称-->
					<spring:message code="category_show_En" />
				</p>
				<input type="text" id="itemNameEn" name="itemNameEn"
					value="${category.itemNameEn}" maxlength="50"
					data-rule-required="true"
					data-msg-required="<spring:message code="category_pleaseInputEn"/>"
					class="form-control fam">
			</div>
			<div class="col-50 right clearfix">
				<!--英文略称-->
				<p class="tit">
					<spring:message code="category_show_abbreviation_En" />
				</p>
				<input type="text" id="itemNameEn" name="itemNameEn"
					value="${category.itemNameEn}" maxlength="50"
					data-rule-required="true"
					data-msg-required="<spring:message code="category_pleaseInput_abbreviationEn"/>"
					class="form-control fam">
			</div>
		</div>
		<div class="items clearfix">
			<div class="col-50 clearfix">
				<!--状态-->
				<p class="tit">
					<spring:message code="category_edit_state" />
				</p>
				<div class="labelBox">
					<!--启用-->
					<label> <input type="radio" name="enableFlag"
						checked='checked' value="1"
						<c:if test="${category.enableFlag eq 1}">checked="checked"</c:if>
						class="rast" /> <spring:message code="common_enable" /> <!--停用-->
					</label> <label><input type="radio" name="enableFlag" value="0"
						<c:if test="${category.enableFlag eq 0}">checked="checked"</c:if>
						class="rast" /> <spring:message code="common_disable" /> </label>
				</div>
			</div>
		</div>
		<div class="items clearfix">
			<div class="clearfix">
				<div class="tit">
					<!--描述-->
					<spring:message code="category_edit_remarks" />
					<p class="textareaTxt">
						<spring:message code="category_note" />
					</p>
				</div>
				<textarea style="width: 518px; height: 130px;" name="remarks"
					value="${category.remarks}">${category.remarks}</textarea>
			</div>

		</div>
	</div>
</body>
</html>
